﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using GpgApi;
using Microsoft.WindowsAPICodePack.Taskbar;
using WgpgCore;

// http://blogs.microsoft.co.il/blogs/arik/archive/2010/03/09/windows-7-taskbar-c-quick-reference.aspx
// http://www.codeproject.com/KB/dotnet/Win7NewFeatures.aspx

namespace WgpgGUI
{
    public partial class WgpgApplication : Application, ISingleInstanceApp
    {
        [STAThread]
        public static void Main()
        {
            if (SingleInstance<WgpgApplication>.InitializeAsFirstInstance("WgpgApplication"))
            {
                WgpgApplication application = new WgpgApplication();
                application.InitializeComponent();
                application.Run();

                SingleInstance<WgpgApplication>.Cleanup();
            }
        }

        public Boolean SignalExternalCommandLineArgs(IList<String> args)
        {
            ExecuteArguments(args);
            return true;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

            base.OnStartup(e);

            GpgInterface.ExePath = Config.Instance.GpgPath;
            GpgInterface.SynchronizationContext = SynchronizationContext.Current;

            if (ExecuteArguments(Environment.GetCommandLineArgs()) && !Config.Instance.KeepOpenOnCommandLine)
                Shutdown();
            else
            {
                KeyManager.Refresh += Refresh;
                TaskManager.Instance.PropertyChanged += TaskManager_PropertyChanged;

                if (TaskbarManager.IsPlatformSupported)
                    TaskbarManager.Instance.ApplicationId = "WgpgTaskbarManaged";

                MainWindow win = new MainWindow();
                win.Show();

                if (TaskbarManager.IsPlatformSupported)
                {
                    TaskbarManager.Instance.SetApplicationIdForSpecificWindow(win, "WgpgWindow");
                    SetJumpList();
                }
            }
        }

        private static void SetJumpList()
        {
            String p = Utils.GetApplicationDirectory();

            JumpList jumpList = JumpList.CreateJumpList();
            IJumpListTask task1 = new JumpListLink(Path.Combine(p, "WgpgGUI.exe"), "Encrypt a File") { Arguments = "-action=encrypt" };
            IJumpListTask task2 = new JumpListLink(Path.Combine(p, "WgpgGUI.exe"), "Decrypt a File") { Arguments = "-action=decrypt" };
            jumpList.AddUserTasks(task1, task2);
            jumpList.Refresh();
        }

        private static Boolean ExecuteArguments(IList<String> args)
        {
            Arguments a = Arguments.Parse(args);

            if (a.Action == "encrypt")
            {
                Encrypt.File(null, a.Input);
                return true;
            }
            
            if (a.Action == "decrypt")
            {
                Decrypt.File(null, a.Input);
                return true;
            }

            return false;
        }

        private static void Refresh(KeyManager.RefreshStatus status)
        {
            Mouse.OverrideCursor = status == KeyManager.RefreshStatus.Start ? Cursors.Wait : null;
        }

        private static void TaskManager_PropertyChanged(Object sender, PropertyChangedEventArgs e)
        {
            if (TaskbarManager.IsPlatformSupported)
            {
                if (TaskManager.Instance.IsRunning)
                    TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Indeterminate);
                else
                    TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
            }
        }
    }
}
